package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TouristComment;
import cn.wolfcode.luowowo.comment.query.TouristCommentQuery;
import cn.wolfcode.luowowo.comment.repository.TouristCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITouristCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TouristCommentServiceImpl implements ITouristCommentService{

    @Autowired
    private TouristCommentRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public TouristComment commentAdd(TouristComment comment) {
        Integer commentCount;
        //判断id是否存在
        if(!StringUtils.hasLength(comment.getId())){

            comment.setCreateTime(new Date());
        }
        //判断引用评论
        if(comment.getType()== TouristComment.TRAVLE_COMMENT_TYPE){
            if(StringUtils.hasLength(comment.getRefComment().getId())){
                comment.setRefComment(repository.findById(comment.getRefComment().getId()).get());
                TouristComment refComment = comment.getRefComment();
                if (refComment.getCommentCount()==null){
                    commentCount = 0;
                }else {
                    commentCount = refComment.getCommentCount();
                }
                commentCount++;
                refComment.setCommentCount(commentCount);
                repository.save(refComment);

            }
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public Long count(Long touristId) {
        //2.查询总条数
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("touristId").is(touristId);
        query.addCriteria(criteria);
        long count = template.count(query, TouristComment.class);
        return count;
    }

    @Override
    public Page query(TouristCommentQuery qo) {
        Query query = new Query();
        if (qo.getTouristId() != -1){
            query.addCriteria(Criteria.where("touristId").is(qo.getTouristId()));
        }
        Long total = template.count(query, TouristComment.class);
        if (total == 0){
            return Page.empty();
        }
        Pageable pageable = new PageRequest(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "creatime");
        query.with(pageable);//??
        List<TouristComment> touristComments = template.find(query, TouristComment.class);

        return new PageImpl(touristComments,pageable,total);

    }

    @Override
    public boolean commentThumbUp(String id, Long userId) {
        TouristComment comment = repository.findById(id).get();
        if (comment == null){
            return false;
        }
        List<Long> thumbuplist = comment.getThumbuplist();
        //如果是点赞操作,+1,将该用户放进点赞用户的集合
        if (!thumbuplist.contains(userId)){ //不包含说明没点赞过
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(userId);
            repository.save(comment);
            return true;
        }else{//如果是取消点赞, 反过来
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(userId);
            repository.save(comment);
            return false;
        }
        //修改操作 update

    }

    @Override
    public List<TouristComment> getById(Long touristId) {
        return  repository.findByTouristId(touristId);
    }

    @Override
    public List<TouristComment> getTouristCommnetByUserId(Long id) {
        List<TouristComment> list = repository.findByUserId(id);
        return list;
    }


}
